package com.pragmatic.controller;

import com.alibaba.fastjson.JSON;
import com.pragmatic.cryptography.EncryptMessage;
import com.pragmatic.cryptography.SDKClient;
import com.pragmatic.domain.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * ...
 *
 * @author wangdong
 * @since 2022/9/28 11:19
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

  @Resource
  private SDKClient sdkClient;

  /**
   * 查询用户信息(明文)
   *
   * @param message 用户对象信息
   */
  @RequestMapping(value = "/getUserInfo/plaintext", method = RequestMethod.POST)
  public String getUserInfoPlaintext(@RequestBody String message) {
    log.info("明文-接受的信息={}", message);
    String response = "我是provider明文demo!!!";
    log.info("明文-响应的信息={}", response);
    return response;
  }

  /**
   * 查询用户信息(密文)
   *
   * @param message 用户对象信息
   */
  @RequestMapping(value = "/getUserInfo/cryptography", method = RequestMethod.POST)
  public String getUserInfoCryptography(@RequestBody String message) {
    log.info("密文-接受的信息={}", message);
    // 验签、解密
    String decrypt = sdkClient.doDecrypt(message);
    log.info("密文-验签/解密后-明文数据={}", decrypt);
    // 加密、加签
    String response = "我是provider密文字符串demo!!!";
    EncryptMessage encryptMessage = sdkClient.doEncrypt(response);
    return JSON.toJSONString(encryptMessage);
  }
}
